﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlueHome.Admin.Models;
using MyMvc.BLL;
using MyMvc.IBLL;
using MyMvc.Model;
using MyMvc.Model.Models;

namespace BlueHome.Admin.Controllers
{
    public class CompanyController : Controller
    {
        INoteService _noteService = new NoteService();
        IRelationService _relationService = new RelationService();
        //
        // GET: /Company/
        //创建一个ListService对象
        //公司简介
        public ActionResult Index()
        {
            return View();
        }
        //联系方式
        public ActionResult Contact()
        {
            //查询出联系方式
            var currentRelation = _relationService.LoadEntities(c => c.ID == 1).FirstOrDefault();

            //做成一个强类型，传递给前台接收
            ViewData.Model = currentRelation;
            return View();
        }
        //修改联系方式
        public ActionResult ModifyRelation(Relation re)
        {
            _relationService.UpdateEntities(re);
            return Content("OK");
        }

        // 留言管理
        public ActionResult MangeNote()
        {
            return View();
        }

        public ActionResult AddNote(Note note)
        {
            _noteService.AddEntities(note);
            return Content("OK");
        }
        //查看留言
        public ActionResult Note(int ID)
        {
            //查询出当前的留言信息
            var currentNote = _noteService.LoadEntities(c => c.ID == ID).FirstOrDefault();

            //做成一个强类型，传递给前台接收
            ViewData.Model = currentNote;
            return View();
        }

        //回复留言
        public ActionResult Reply()
        {
            return Content("OK");
        }
       
        /// <summary>
        /// 获得所有留言数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllNote()
        {
            //分页的数据
            //
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);

            //封装一个业务逻辑层，来处理分页过滤的事件
            GetModelQuery noteInfoQuery = new GetModelQuery();
            noteInfoQuery.pageIndex = pageIndex;
            noteInfoQuery.pageSize = pageSize;
            noteInfoQuery.total = 0;
            //放置依赖刷新
            var data = from u in _noteService.LoadSearchData(noteInfoQuery)
                       select new { u.ID,u.Topic, u.Position, u.People,u.Phone,u.Website,u.Mail,u.Words,u.Type };

            //var data = _userInfoService.LoadPagerEntities(pageSize, pageIndex, out total, u => true, true, u => u.ID);

            var result = new { total = noteInfoQuery.total, rows = data };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="deleteID"></param>
        /// <param name="Notelist"></param>
        /// <returns></returns>
        public ActionResult DeleteNoteInfo(string deleteID, string Notelist)
        {

            if (string.IsNullOrEmpty(deleteID))
            {
                return Content("请选择您要删除的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (_noteService.DeleteNote(deleteIDList) > 0)
            {
                return Content("OK");
            }
            return Content("删除失败，请您检查");

        }

        
      

    }
}
